import { COMMIT_COURSE, GET_COURSE, GET_COURSES } from "@/graphql/course";
import { DEFAULT_PAGE_SIZE } from "@/utils/contants";
import type {
  TBaseCourse,
  TCourseQuery,
  TCoursesQuery,
} from "@/utils/types";
import { useLazyQuery, useMutation, useQuery } from "@apollo/client";
import { message } from "antd";

export const useCourses = (
  pageNum = 1,
  pageSize = DEFAULT_PAGE_SIZE
  // isSample = false
) => {
  const { loading, data, refetch } = useQuery<TCoursesQuery>(GET_COURSES, {
    skip: true, //因为主要是由refetch触发，这里需要这样配置，避免两次请求
    variables: {
      page: {
        pageNum,
        pageSize,
      },
    },
  });

  const refetchHandler = async (params: {
    name?: string;
    pageSize?: number;
    current?: number;
  }) => {
    const { data: res, error } = await refetch({
      name: params.name,
      page: {
        pageNum: params.current,
        pageSize: params.pageSize,
      },
    });
    if (error) {
      return {
        success: false,
      };
    }
    return {
      success: true,
      total: res?.getCourses.page.total,
      data: res?.getCourses.data,
    };
  };

  return {
    loading,
    refetch: refetchHandler,
    page: data?.getCourses.page,
    data: data?.getCourses.data,
  };
};
export const useCoursesForSample = (
  // pageNum = 1,
  // pageSize = DEFAULT_PAGE_SIZE
  // isSample = false
) => {
  const [get, {data,loading}] = useLazyQuery<TCoursesQuery>(GET_COURSES)
  const searchHandler=(name:string)=>{
    get({
      variables:{
        name,
        page:{
          pageNum:1,
          pageSize:DEFAULT_PAGE_SIZE,
        }
      }
    })
  }

  return {
    data:data?.getCourses.data,
    search:searchHandler,
    loading,
  }
};

export const useEditCourseInfo = (): [
  handleEdit: (
    id: string,
    params: TBaseCourse,
    callback?: () => void
  ) => Promise<void>,
  loading: boolean
] => {
  const [edit, { loading }] = useMutation(COMMIT_COURSE);

  const handleEdit = async (
    id: string,
    params: TBaseCourse,
    callback?: (isReload: boolean) => void
  ) => {
    const res = await edit({
      variables: {
        id,
        params,
      },
    });
    if (res.data.commitCourse.code === 200) {
      message.success(res.data.commitCourse.message);
      if(callback)
        callback(true);
      return;
    }

    message.error(res.data.commitCourse.message);
  };

  return [handleEdit, loading];
};

export const useCourse = () => {
  // const { loading, data } = useQuery<TCourseQuery>(GET_COURSE, {
  //   skip: true,
  //   variables: {
  //     id,
  //   },
  // });

  // 手动触发请求
  const [get, { loading }] = useLazyQuery(GET_COURSE);
  const getCourse = async (id: string) => {
    const res = await get({
      variables: {
        id,
      },
    });
    return res.data.getCourseInfo.data;
  };
  return {
    loading,
    getCourse,
  };
};
export const useCourseInfo = (id:string) => {
  const { loading, data,refetch } = useQuery<TCourseQuery>(GET_COURSE, {
    // skip: true,
    variables: {
      id,
    },
  });

  return {
    loading,
    refetch,
    data:data?.getCourseInfo.data,
  };
};
